package com.lz.sports.ui.group

import android.os.Bundle
import android.view.View
import androidx.lifecycle.lifecycleScope
import androidx.navigation.fragment.findNavController
import com.lz.library.base.BaseFragment
import com.lz.library.extension.click
import com.lz.library.extension.resString
import com.lz.library.extension.showToast
import com.jeremyliao.liveeventbus.LiveEventBus
import com.lz.sports.BR
import com.lz.sports.R
import com.lz.sports.adapter.DeviceGroupEditAdapter
import com.lz.sports.bean.DeviceGroup
import com.lz.sports.bean.DeviceInfo
import com.lz.sports.databinding.FragmDeviceGroupEditBinding
import com.lz.sports.dialog.DelayedLoadingDialog
import com.lz.sports.dialog.DeviceNumDialog
import com.lz.sports.dialog.EditPublicDialog
import com.lz.sports.vm.group.DeviceGroupEditViewModel
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch

//设备分组编辑列表
class DeviceGroupEditFragment : BaseFragment<DeviceGroupEditViewModel, FragmDeviceGroupEditBinding>(),
    EditPublicDialog.OnEditConfirmListener, DeviceNumDialog.OnNumEditListener {

    private val adapter by lazy { DeviceGroupEditAdapter() }

    override fun getLayoutRes(): Int = R.layout.fragm_device_group_edit

    override fun getVMVariableId(): Int = BR.vm

    override fun initObserve() {
        viewModel.clearEvent.observe(this) {
            if (it) adapter.removeAll()
        }
        viewModel.delayEvent.observe(this) {
            DelayedLoadingDialog.show(this, it)
        }

        LiveEventBus.get("DeviceGroup_bindDevice", DeviceInfo::class.java).observe(this) {
            if (viewModel.bindDeviceInfo(it)) {
                adapter.addItem(item = it)
            }
        }
    }

    override fun initData(savedState: Bundle?) {
        binding.rvList.adapter = adapter
        adapter.itemMenuListener = object : DeviceGroupEditAdapter.OnItemMenuListener {
            override fun onItemEdit(position: Int) {
                DeviceNumDialog.show(this@DeviceGroupEditFragment, position, adapter.getItem(position))
            }

            override fun onItemUnbind(position: Int) {
                if (viewModel.unbind(adapter.getItem(position))) {
                    adapter.removeItem(position)
                }
            }
        }
        @Suppress("DEPRECATION")
        val deviceGroup = arguments!!.getParcelable<DeviceGroup>("deviceGroup")!!
        viewModel.deviceGroup.value = deviceGroup
        adapter.setNewData(deviceGroup.deviceList())

        lifecycleScope.launch {
            arrayOf(binding.tvModify, binding.tvConnect, binding.tvUnbind, binding.tvSelect, binding.tvInfo)
                .click().collect { onClick(it) }
        }
    }

    override fun onEditConfirm(dialog: EditPublicDialog, text: String, requestCode: Int) {
        dialog.dismiss()
        viewModel.modifyName(text)
    }

    private fun onClick(v: View) {
        when (v.id) {
            R.id.tv_modify -> EditPublicDialog.show(
                this,
                R.string.group_name.resString(),
                R.string.group_modify_name.resString(),
                viewModel.deviceGroup.value!!.name
            )
            R.id.tv_connect -> viewModel.connect()
            R.id.tv_unbind -> viewModel.unbindAll()
            R.id.tv_info -> viewModel.battery()
            R.id.tv_select -> {
                val bundle = Bundle().apply { putParcelable("DeviceGroup", viewModel.deviceGroup.value) }
                findNavController().navigate(R.id.action_group_edit_to_bind, bundle)
            }
        }
    }

    override fun onNumEdit(dialog: DeviceNumDialog, number: Int, position: Int) {
        val deviceInfo = adapter.getItem(position)
        if (deviceInfo.number == number) return
        if (number > 0 && viewModel.modifyNumber(number, deviceInfo)) {
            dialog.dismiss()
            adapter.notifyItemChanged(position)
        } else {
            R.string.group_invalid_num.showToast()
        }
    }
}